package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.GoodsModel;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.utils.ResUtils;
import com.zw.yzk.kabu.view.activity.GoodsDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/6.
 */

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.Holder> {

    private List<GoodsModel> goodList;
    private Context context;


    public GoodsListAdapter(List<GoodsModel> list) {
        this.goodList = new ArrayList<>();
        if (list != null) {
            this.goodList.addAll(list);
        }
    }

    public void refresh(List<GoodsModel> list) {
        this.goodList.clear();
        if (list != null) {
            this.goodList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<GoodsModel> list) {
        if (list != null) {
            int start = goodList.size();
            this.goodList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_goods_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final GoodsModel model = goodList.get(position);
        holder.name.setText(model.getName());
        holder.seller.setText(model.getShopName());
        holder.price.setText(ResUtils.formatCurrency(context, model.getPrice()));
        int width = (int) ((PhoneUtils.getScreenWidth(context) - 30 * context.getResources().getDisplayMetrics().density) / 2f);
        ViewGroup.LayoutParams params = holder.image.getLayoutParams();
        if (params == null) {
            int defaultSize = (int) (170 * context.getResources().getDisplayMetrics().density);
            params = new ViewGroup.LayoutParams(defaultSize, defaultSize);
        }
        params.width = width;
        params.height = width;
        holder.image.setLayoutParams(params);
        Glide.with(context)
                .load(model.getImage())
                .centerCrop()
                .error(R.drawable.im_default)
                .into(holder.image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsDetailsActivity.class);
                intent.putExtra(ActivityConstant.GOODS_DETAILS_ID, model.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.good_image)
        ImageView image;
        @Bind(R.id.good_name)
        TextView name;
        @Bind(R.id.seller)
        TextView seller;
        @Bind(R.id.price)
        TextView price;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
